<?php
namespace app\admin\controller;
use app\admin\model\Equipment as EquipmentModel;
use app\admin\model\WebSet;

/**
 * 打印设备授权控制器
 */
class Equipment extends Controller
{
    /**
     * 设备列表
     */
    public function index()
    {
        $model = new EquipmentModel;
        $list = $model->getList();
        return $this->fetch('index', compact('list'));
    }

    /**
     * 添加
     */
    public function add()
    {
        if (!$this->request->isAjax()) {
            return $this->fetch('add');
        }
		//测试判断
		if($this->admin['user']['config_id']==0){
			return $this->renderError('测试用户不可操作');
		}
		$model = new EquipmentModel;
        if ($model->add($this->postData('equipment'))) {
            return $this->renderSuccess('添加成功', url('equipment/index'));
        }
        $error = $model->getError() ?: '添加失败';
        return $this->renderError($error);
    }

    /**
     * 删除
     */
    public function delete($equipment_id)
    {
		//测试判断
		if($this->admin['user']['config_id']==0){
			return $this->renderError('测试用户不可操作');
		}
        $model = EquipmentModel::detail(['equipment_id' => $equipment_id]);
        if (!$model->remove()) {
            return $this->renderError('删除失败');
        }
        return $this->renderSuccess('删除成功');
    }

    /**
     * 编辑
     */
    public function edit($equipment_id)
    {
        // 详情
        $model = EquipmentModel::detail(['equipment_id' => $equipment_id]);
        if (!$this->request->isAjax()) {
            return $this->fetch('edit', compact('model'));
        }
		//测试判断
		if($this->admin['user']['config_id']==0){
			return $this->renderError('测试用户不可操作');
		}
        // 更新记录
        if ($model->edit($this->postData('equipment'))) {
            return $this->renderSuccess('更新成功', url('equipment/index'));
        }
        $error = $model->getError() ?: '更新失败';
        return $this->renderError($error);
    }
	
	/**
     * 云端设置
     */
    public function setting()
    {
		if (!$this->request->isAjax()) {
            $values = WebSet::getItem('printer');
            return $this->fetch('setting', compact('values'));
        }
		//测试判断
		if($this->admin['user']['config_id']==0){
			return $this->renderError('测试用户不可操作');
		}
        $model = new WebSet;
        if ($model->edit('printer',$this->postData('printer'))) {
            return $this->renderSuccess('更新成功');
        }
        return $this->renderError('更新失败');
    }

}
